'use strict'

const { ApiController } = require('jax')

class AdminRoleController extends ApiController {
  get Model() {
    return this.ctx.model.AdminRole
  }

  // 必填字段
  get required() {
    return ['roleName', 'roleDesc']
  }

  // 可提交字段
  get include() {
    return [...this.required, 'menuIds', 'systemType', 'isOpen']
  }

  // 返回JSON字段
  get json() {
    return [...this.include, 'id', 'isAdmin', 'createdAt', 'isAdmin', 'createdAt', 'systemTypeDesc']
  }

  // 匹配查询
  get searchKey() {
    return ['isOpen']
  }

  // 获取查询条件
  getFilter() {
    const { ctx } = this
    const { query, helper } = ctx
    const { admin } = ctx.state
    const filter = {}

    if (admin.isAdmin) {
      filter.systemType = query.systemType || admin.systemType
    } else {
      filter.systemType = admin.systemType
    }

    return filter
  }

  // 分类树
  async tree() {
    const { ctx } = this
    const { helper } = ctx
    const filter = this.getFilter()

    const tree = await this.Model.tree({
      filter,
    })

    helper.tree.eachTree(tree, (item) => this.toJson(item))

    ctx.body = {
      code: 0,
      msg: 'success',
      data: tree,
    }
  }

  // 查询前
  beforeIndex() {
    const { ctx } = this
    const { admin } = ctx.state
    const { query } = ctx
    const filter = this.getFilter()

    return {
      filter,
      sort: {
        _id: 'ASC',
      },
    }
  }

  // 新增前
  async beforeCreate() {
    const { ctx } = this
    const { body } = ctx.request
    const { admin } = ctx.state

    // 设置角色来源
    // body.sourceId = admin.id
    // body.sourceType = admin.sourceType.

    if (admin.isAdmin) {
      body.systemType = body.systemType || admin.systemType
    } else {
      body.systemType = admin.systemType
    }
  }

  // 修改前
  async beforeUpdate() {
    const { ctx } = this
    const { body } = ctx.request
    const { admin } = ctx.state

    if (body.sourceType != undefined && body.sourceType != admin.sourceType) throw '越级操作'
  }
}

module.exports = AdminRoleController

/**
   * @api {POST} /system/role [角色] 新增
   * @apiUse AdminCommon
   * @apiGroup Role
   * 
   * @apiBody {String} roleName 角色名称
   * @apiBody {String} roleDesc 角色描述
   * @apiBody {Array} menuIds 勾选菜单
   * @apiBody {Boolean} [isOpen] 启用角色
   *
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "操作成功"
  }
*/

/**
   * @api {PUT} /system/role/:roleId [角色] 修改
   * @apiUse AdminCommon
   * @apiGroup Role
   * 
   * @apiBody {String} [roleName] 角色名称
   * @apiBody {String} [roleDesc] 角色描述
   * @apiBody {Array} [menuIds] 勾选菜单
   * @apiBody {Number} [ticketType=0] 票种类型 0、呀诺达景区门票 1、索道票
   * @apiBody {Boolean} [isOpen] 启用角色
   *
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "操作成功"
  }
  */
/**
   * @api {GET} /system/role [角色] 列表
   * @apiUse AdminCommon
   * @apiGroup Role
   * @apiQuery {String} [page=1] 页码
   * @apiQuery {String} [pageSize=20] 条数
   *
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": {
          "total": 2,
          "list": [
              {
                  "roleId": "63fdfc35896a361078819dba", // 角色ID
                  "roleName": "超级管理员", // 角色名称
                  "roleDesc": "超级管理员", // 角色描述
                  "createdAt": "2023-02-28 21:05:57", // 创建时间
                  "isAdmin": true, // 超级管理员（不可删除、编辑）
                  "isOpen": true, // 角色是否启用
              }
          ],
          "maxPage": 1,
          "page": 1,
          "pageSize": 20
      }
  }
*/
/**
   * @api {GET} /system/role/:roleId [角色] 详情
   * @apiUse AdminCommon
   * @apiGroup Role
   *
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": {
        "roleId": "63fdfc35896a361078819dba", // 角色ID
        "roleName": "超级管理员", // 角色名称
        "roleDesc": "超级管理员", // 角色描述
        "createdAt": "2023-02-28 21:05:57", // 创建时间
        "isAdmin": true, // 超级管理员（不可删除、编辑）
      }
  }
*/

/**
   * @api {DELETE} /system/role/:roleId [角色] 删除
   * @apiUse AdminCommon
   * @apiGroup Role
   *
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "操作成功"
  }
*/
